package megrez.mail.sender.domain;

import java.util.Locale;

import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
import megrez.mail.sender.utils.ContentNotFoundException;
import megrez.mail.sender.utils.TemplateNotFoundException;

/**
 * 查询服务
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@Service
@RequiredArgsConstructor
public class QueryService {
    private final TemplateConfigRepository templateRep;
    private final TemplateContentRepository contentRep;

    public TemplateConfig findConfigById(String configId) {
        return templateRep.findById(configId).orElseThrow(() -> new TemplateNotFoundException(configId));
    }

    public TemplateConfig findConfigByCode(String code) {
        return templateRep.findOneByCode(code).orElseThrow(() -> new TemplateNotFoundException(code));
    }

    public TemplateContent findContentById(String contentId) {
        return contentRep.findById(contentId).orElseThrow(() -> new ContentNotFoundException(contentId));
    }
    /**
     * 查询合适的模板内容
     * 
     * @param templateId
     * @param locale
     * @return
     */
    public TemplateContent findContent(TemplateConfig template, Locale locale) {
        return contentRep.findOneByTemplateConfigAndLocale(template, locale)
                .orElseThrow(() -> new ContentNotFoundException(template.getCode()));
    }

}
